﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using util;
using common;
namespace dao
{
    public class UserGroupDAO
    {
        public DataTable getAllUserGroup()
        {
            DataTable dt = new DataTable();
            try
            {
                DataUtil.connect();
                String cmdText = "sp_getAllUserGroup";
                dt = DataUtil.executeStore(cmdText, null);
            }
            catch (Exception ex)
            {

            }
            return dt;
        }
        public DataTable getUserGroupByID(String idLogin)
        {
            DataTable dt = new DataTable();
            try
            {
                DataUtil.connect();
                String cmdText = "sp_getUserGroupByID";
                MySqlParameter[] arrParam = new MySqlParameter[1];
                arrParam[0] = new MySqlParameter("_IDGroup", idLogin);
                dt = DataUtil.executeStore(cmdText, arrParam);
            }
            catch (Exception ex)
            {
                String meg = ex.Message;
            }
            return dt;
        }
        public DataTable getAllUserGroupPermission(String idLogin, String idPermissionType)
        {
            DataTable dt = new DataTable();
            try
            {
                DataUtil.connect();
                String cmdText = "sp_getAllUserGroupPermissionByIDGroup";
                MySqlParameter[] arrParam = new MySqlParameter[2];
                arrParam[0] = new MySqlParameter("_IDGroup", idLogin);
                arrParam[1] = new MySqlParameter("_IDPermissionType", idPermissionType);
                dt = DataUtil.executeStore(cmdText, arrParam);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return dt;
        }
        public Boolean updateUserGroupPermission(String cmdText, String groupName)
        {
            Boolean result = false;
            try
            {
                DataUtil.connect();
                MySqlParameter[] arrParam = new MySqlParameter[4];
                arrParam[0] = new MySqlParameter("_cmdText", cmdText);
                arrParam[1] = new MySqlParameter("_IDLog", Constant.LOG_ID);
                arrParam[2] = new MySqlParameter("_Actions", Constant.DR_ACTIONS_VARIABLE["varUpdate"]);
                arrParam[3] = new MySqlParameter("_Contents", "Permissions of " + groupName);
                cmdText = "sp_UpdateUserGroupPermission";
                result = DataUtil.executeNonQuery(cmdText, arrParam);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
        public Boolean updateUserGroupPermissionbyColumn(String typeColumn, String idGroup, String idType, String status, String groupName)
        {
            Boolean result = false;
            try
            {
                DataUtil.connect();
                MySqlParameter[] arrParam = new MySqlParameter[7];
                arrParam[0] = new MySqlParameter("_TypeColumn", typeColumn);
                arrParam[1] = new MySqlParameter("_IDGroup", idGroup);
                arrParam[2] = new MySqlParameter("_IDType", idType);
                arrParam[3] = new MySqlParameter("_Status", status);
                arrParam[4] = new MySqlParameter("_IDLog", Constant.LOG_ID);
                arrParam[5] = new MySqlParameter("_Actions", Constant.DR_ACTIONS_VARIABLE["varUpdate"]);
                arrParam[6] = new MySqlParameter("_Contents", "Permissions of " + groupName);
                String cmdText = "sp_updateUserGroupPermissionbyColumn";
                result = DataUtil.executeNonQuery(cmdText, arrParam);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
        public Boolean insertUserGroup(String groupName, String descriptions)
        {
            Boolean result = false;
            try
            {
                DataUtil.connect();
                MySqlParameter[] arrParam = new MySqlParameter[5];
                arrParam[0] = new MySqlParameter("_GroupName", groupName);
                arrParam[1] = new MySqlParameter("_Description", descriptions);
                arrParam[2] = new MySqlParameter("_IDLog", Constant.LOG_ID);
                arrParam[3] = new MySqlParameter("_Actions", Constant.DR_ACTIONS_VARIABLE["varInsert"]);
                arrParam[4] = new MySqlParameter("_Contents", "User group");
                String cmdText = "sp_insertUserGroup";
                result = DataUtil.executeNonQuery(cmdText, arrParam);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
    }
}
